import * as Cesium from 'cesium'

const Switch = function(viewer,options){
    this.viewer = viewer;
    this.options = options
    this.initialize(viewer,options)
}
Switch.prototype = {

    initialize: function (viewer, options) {
        options = this._setOptions(this, options)
        if (!options.dataSource || options.dataSource.length <= 0) {
            console.log('options内的dataSource是必要参数');
        }
        this.viewer = viewer
        this.loadDataSoure(options.dataSource)
    },

    loadDataSoure: function (dataSource) {
        this.options.dataSource = dataSource;
        let viewer = this.viewer
        Cesium.GeoJsonDataSource.load(dataSource).then(function (data) {
            viewer.dataSources.add(data);

            var entities = data.entities.values;

            for (var i = 0; i < entities.length; i++) {
                var entity = entities[i];
                //拿到经纬度坐标

                var cartographic = Cesium.Cartographic.fromCartesian(entity.position.getValue());

                var lon = Cesium.Math.toDegrees(cartographic.longitude);
                var lat = Cesium.Math.toDegrees(cartographic.latitude);

                // //改变高程
                // entity.position = Cesium.Cartesian3.fromDegrees(lon, lat, 297.12)
                entity.billboard = {
                    image: './static/images/tips.png',
                    scale: 0.3
                };
                
                // entity.point = new Cesium.PointGraphics({
                //   color: Cesium.Color.RED,
                //   pixelSize: 10
                // });
            }
        }).otherwise(error=>{
            console.log(error);
            
        });
    },
    _setOptions: function (obj, options) {
        if (!obj.hasOwnProperty('options')) {
            obj.options = obj.options ? create(obj.options) : {};
        }
        for (var i in options) {
            obj.options[i] = options[i];
        }
        return obj.options;
    },
}

export const switchWidget = function(viewer,options){
    var widget = new Switch(viewer,options)
}